package cn.gz.demo.ui.recylerview_header;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.gz.demo.R;
import cn.gz.demo.databinding.RecylerViewBinding;
import cn.gz.demo.ui.base.BaseActivity;

/**
 * Created by Liuyz on 2018/8/10.
 */

public class RecylerViewGridActivity extends BaseActivity {
    private RecylerViewBinding binding;
    private List<String> list;
    private RecyclerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);

        initView();
        initData();
    }

    private void initView() {

        final GridLayoutManager manager = new GridLayoutManager(this,3);
        binding.recyclerview.setLayoutManager(manager);
        adapter = new RecyclerAdapter();
        binding.recyclerview.setAdapter(adapter);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return adapter.isFoot(position) || adapter.isHead(position) ? manager.getSpanCount() : 1;
            }
        });
    }

    private void initData() {
        list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add("item "+i);
        }
        adapter.notifyDataSetChanged();
    }

    private class RecyclerAdapter extends RecyclerView.Adapter{

        private final static int HEAD_COUNT = 1;
        private final static int FOOT_COUNT = 1;

        private final static int TYPE_HEAD = 0;
        private final static int TYPE_CONTENT = 1;
        private final static int TYPE_FOOTER = 2;

        public int getContentSize(){
            return list.size();
        }

        public boolean isHead(int position){
            return HEAD_COUNT != 0 && position == 0;
        }

        public boolean isFoot(int position){
            return FOOT_COUNT != 0 && position == getContentSize() + HEAD_COUNT;
        }

        @Override
        public int getItemViewType(int position) {
            int contentSize = getContentSize();
            if (HEAD_COUNT != 0 && position == 0){ // 头部
                return TYPE_HEAD;
            }else if(FOOT_COUNT != 0 && position == HEAD_COUNT + contentSize){ // 尾部
                return TYPE_FOOTER;
            }else{
                return TYPE_CONTENT;
            }
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == TYPE_HEAD){
                View itemView = LayoutInflater.from(RecylerViewGridActivity.this).inflate(R.layout.head_for_recyclerview,parent,false);
                return new RecyclerAdapter.HeadHolder(itemView);
            }else if(viewType == TYPE_CONTENT){
                View itemView = LayoutInflater.from(RecylerViewGridActivity.this).inflate(R.layout.item_for_recycler_view,parent,false);
                return new RecyclerAdapter.ContentHolder(itemView);
            }else{
                View itemView = LayoutInflater.from(RecylerViewGridActivity.this).inflate(R.layout.foot_for_recyclerview,parent,false);
                return new RecyclerAdapter.FootHolder(itemView);
            }
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof RecyclerAdapter.HeadHolder){ // 头部

            }else if(holder instanceof RecyclerAdapter.ContentHolder){ // 内容
                RecyclerAdapter.ContentHolder myHolder = (RecyclerAdapter.ContentHolder) holder;
                myHolder.itemText.setText(list.get(position - 1));
            }else{ // 尾部

            }
        }

        @Override
        public int getItemCount() {
            return list.size() + HEAD_COUNT + FOOT_COUNT;
        }

        // 头部
        private class HeadHolder extends RecyclerView.ViewHolder{
            public HeadHolder(View itemView) {
                super(itemView);
            }
        }

        // 内容
        private class ContentHolder extends RecyclerView.ViewHolder{
            TextView itemText;
            public ContentHolder(View itemView) {
                super(itemView);
                itemText = (TextView)itemView.findViewById(R.id.item_text);
            }
        }

        // 尾部
        private class FootHolder extends RecyclerView.ViewHolder{
            public FootHolder(View itemView) {
                super(itemView);
            }
        }

    }
}
